import request from '/@/utils/request'

const apiPrefix = '/api/face-recognition/baidu'

interface FaceSearchResponse {
  error_code: number
  error_msg: string
  result?: {
    face_token: string
    user_list: Array<{
      group_id: string
      user_id: string
      user_info: string
      score: number
    }>
  }
}

interface FaceDetectResponse {
  error_code: number
  error_msg: string
  result?: {
    face_num: number
    face_list: Array<{
      face_token: string
      location: {
        left: number
        top: number
        width: number
        height: number
      }
    }>
  }
}

interface ApiResponse<T = any> {
  code: number
  message?: string
  result?: T
}

interface UserData {
  userId: string
  userName?: string
}

interface FaceCheckInData {
  activityId: string
  image: string
}

/**
 * 人脸检测
 * @param image Base64 图片数据
 */
export const detectFace = (image: string): Promise<FaceDetectResponse> => {
  return request({
    url: `${apiPrefix}/jiancha`,
    method: 'post',
    data: {
      image,
    },
  })
}

/**
 * 人脸搜索
 * @param image Base64 图片数据
 */
export const searchFace = (image: string): Promise<ApiResponse<UserData>> => {
  return request({
    url: `${apiPrefix}/sousuo`,
    method: 'post',
    data: {
      image,
    },
  })
}

/**
 * 人脸签到
 * @param data 签到数据，包含活动ID和图片数据
 */
export const faceCheckIn = (data: FaceCheckInData): Promise<ApiResponse<UserData>> => {
  return request({
    url: `/api/checkins/faceCheckIn`,
    method: 'post',
    data,
  })
}

/**
 * 人脸注册
 * @param faceImage Base64 图片数据
 * @param employeeUserId 用户ID
 */
export const registerFace = (data: any) => {
  return request({
    url: `${apiPrefix}/zhuce`,
    method: 'post',
    data,
  })
}

/**
 * 删除人脸
 * @param faceId 用户ID
 * @param employeeUserId 红有达用户id，不是必传，传的话就是删除这人所有的人脸
 */
export const deleteFace = (data: any) => {
  return request({
    url: `${apiPrefix}/delete`,
    method: 'delete',
    data,
  })
}
/**
 * 获取用户人脸列表
 * @param userId 用户ID
 */
export const getFaceList = (userId: string) => {
  return request({
    url: `${apiPrefix}/user/list?employeeUserId=${userId}`,
    method: 'get',
  })
}
